package com.frank.service.nettydemo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @Author: huweifeng
 * @Date: 2025/2/6
 * @Description:
 **/
public class HelloServer {
    public static void main(String[] args) {
        // 1、启动器：负责组装netty组件，启动服务器
        ChannelFuture channelFuture = new ServerBootstrap()
                // 2、添加处理器组
                .group(new NioEventLoopGroup())
                // 3、选择服务器的 ServerSocketChannel的实现
                .channel(NioServerSocketChannel.class)
                // 4、每个Channel的处理
                .childHandler(
                        new ChannelInitializer<NioSocketChannel>() {
                            // 5、子处理器的初始化
                            @Override
                            protected void initChannel(NioSocketChannel ch) throws Exception {
                                ch.pipeline().addLast(new StringDecoder());
                                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                                    @Override
                                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                        System.out.println(msg);
                                    }
                                });
                            }
                        }
                )
                .bind(9090);
        channelFuture.channel().writeAndFlush("server-client:heart=111");

    }
}
